/*----------------------------------------------------------------
 *  Author:        Xilan Ding
 *  Written:       02/10/2014
 *  Last updated:  02/10/2014
 *  
 *  This is the controller interacting with all other classes defined 
 *  in this project to fulfill certain functionalities.
 *
 *----------------------------------------------------------------*/
package JavaSource;

import java.io.Serializable;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Xilan Ding
 */
public class FoodOrderingSystem implements Serializable {

    //attribute
    private LinkedList<Member> memberList;
    private LinkedList<Customer> custList;
    private LinkedList<Employee> employeeList;
    private LinkedList<Order> orderList;
    private LinkedList<Dish> dishList;
    private LinkedList<Coupon> couponList;
    private LinkedList<Ingredient> ingredientList;
    private LinkedList<Payment> paymentList;
   

    private int idSet[] ;
    public FoodOrderingSystem() {
        memberList=new LinkedList<Member>();
        custList=new LinkedList<Customer>();
        employeeList=new LinkedList<Employee>();
        orderList=new LinkedList<Order>();
        dishList=new LinkedList<Dish>();
        couponList=new LinkedList<Coupon>();
        ingredientList=new LinkedList<Ingredient>();
        paymentList=new LinkedList<Payment>();
        idSet = new int[7];
    }
    
    
    
    public FoodOrderingSystem loadSystem() throws IOException {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("FoodOrderingSystem.dat");
            ois = new ObjectInputStream(fis);
            FoodOrderingSystem FOS = (FoodOrderingSystem) (ois.readObject());
            idSet = FOS.idSet;
            for(int i=0;i<=6;i++){System.out.println(idSet[i]);}
            
            Coupon.setGlobalId(idSet[0]);            
            Customer.setGlobalId(idSet[1]);
            Dish.setGlobalId(idSet[2]);
            Employee.setGlobalId(idSet[3]);
            Ingredient.setGlobalId(idSet[4]);
            Order.setGlobalId(idSet[5]);
            Payment.setGlobalId(idSet[6]);
            
            return FOS;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FoodOrderingSystem.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FoodOrderingSystem.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ois.close();
            fis.close();
        }
        return null;

    }
    
    public LinkedList<Order> getDeliverOrder(){
        LinkedList<Order> list = new LinkedList<Order>();
        for(Order order:orderList){
           if(order.getOrderStatus().equals("Ready for Delivery")
                   &&order.getOrderType()==0)
               list.add(order);
        }
        return list;
    }
    

    public boolean saveSystem(FoodOrderingSystem FOS) throws IOException {
        idSet[0] = Coupon.getGlobalId();
        idSet[1] = Customer.getGlobalId();
        idSet[2] = Dish.getGlobalId();
        idSet[3] = Employee.getGlobalId();
        idSet[4] = Ingredient.getGlobalId();
        idSet[5] = Order.getGlobalId();
        idSet[6] = Payment.getGlobalId();
 //for(int i=0;i<=6;i++){System.out.println(idSet[i]);}
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {

            fos = new FileOutputStream("FoodOrderingSystem.dat");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(FOS);

        } catch (Exception ex) {
            Logger.getLogger(FoodOrderingSystem.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            oos.close();
            fos.close();

        }
        return true;

    }

  

    public LinkedList<Payment> getPaymentList() {
        return paymentList;
    }

    public void setPaymentList(LinkedList<Payment> paymentList) {
        this.paymentList = paymentList;
    }

    public LinkedList<Ingredient> getIngredientList() {
        return ingredientList;
    }

    public void setIngredientList(LinkedList<Ingredient> ingredientList) {
        this.ingredientList = ingredientList;
    }

    public LinkedList<Member> getMemberList() {
        return memberList;
    }

    public LinkedList<Customer> getCustList() throws FileNotFoundException, IOException, ClassNotFoundException {

        return custList;
    }

    public LinkedList<Employee> getEmployeeList() throws FileNotFoundException, IOException, ClassNotFoundException {

        return employeeList;
    }

    public LinkedList<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(LinkedList<Order> orderList) {
        this.orderList = orderList;
    }

    public LinkedList<Dish> getDishList() {
        return dishList;
    }

    public void setDishList(LinkedList<Dish> dishList) {
        this.dishList = dishList;
    }

    public LinkedList<Coupon> getCouponList() {
        return couponList;
    }

    public void setCouponList(LinkedList<Coupon> couponList) {
        this.couponList = couponList;
    }


    //method
    public Customer searchCust(int id) {
        //match the typed ID and password
        for(Customer c: custList)
        {
            if(c.getCustomerID()==id )
                return c;
        }
        return null;
    }

    public Employee searchEmployee(int id) {
        for (int i = 0; i < employeeList.size(); i++) {
            Employee e = employeeList.get(i);
            if (e.getEmployeeId() == id) {
                return e;
            }
        }
        return null;
    }
    
    public Dish searchDish(String name) {
        for (int i =0; i < dishList.size(); i++ ){
            Dish d = dishList.get(i);
            if (d.getName().equals(name)) {
                return d;
            }
        }
        return null;
    }

    public void addDishToMenu(Dish dish) {
        //add a new dish to the menu
        dishList.add(dish);
        
    }


    public LinkedList<Dish> getRecommendation() {
       Collections.sort(dishList,Collections.reverseOrder());
       return dishList;
    }

    public void addCust(Customer cust) {
        custList.add(cust);
    }

    public Coupon searchCoupon(String code) {
        for (Coupon coup : couponList) {
            if (coup.getCouponCode().equalsIgnoreCase(code)) {
                return coup;
            }
        }
        return null;
    }// search coupon's existence

    public Employee validateEmployeeLogIn(String username, String password) {
        for (Employee em : employeeList) {
            if (em.getName().equals(username) && em.getPassword().equals(password)) {
                return em;
            }
        }
        return null;
    }

    public Member validateMemberLogIn(String username, String password) {
        for (Customer m : custList) {
            if (m.getEmail().equalsIgnoreCase(username) && m.getPassword().equals(password)) {
                if (m instanceof Member) {
                    return (Member) m;
                }
            }
        }
        return null;
    }
    //Get the order list with status of "Pending", Added by Xilan
    public LinkedList<Order> getPendingOrderList(){
         
         LinkedList<Order> pendingOrderList = new LinkedList<Order>();
         for(Order order: this.orderList){
             if(order.getOrderStatus().equals("Pending")){
                 pendingOrderList.add(order);
             }        
         }
         return pendingOrderList;
    }
    //Find the order with order id, Added by Xilan
    public Order searchOrder(int id) {
        for (int i = 0; i < orderList.size(); i++) {
            Order order = orderList.get(i);
            if (order.getOrderID() == id) {
                return order;
            }
        }
        return null;
    }
    
    
   
}
